#include "journalwindow.h"

#include <KApplication>
#include <KLocale>
#include <KActionCollection>
#include <KStandardAction>
#include <KAction>
#include <KToolBar>

#include "maincontrol.h"
#include "journaltabwidget.h"

JournalWindow::JournalWindow(QWidget *parent) : KXmlGuiWindow(parent)
{
    m_control = new MainControl(this);
    m_tabWidget = 0;
    m_toolActionGroup = 0;
    m_penSizeGroup = 0;
    setupActions();
    configureTabWidget();
    setupGUI(Default, "knoteui.rc");
    connect(this, SIGNAL(windowLoaded()), m_control, SLOT(onWindowLoaded()));
}

void JournalWindow::show()
{
    KXmlGuiWindow::show();
    emit windowLoaded();
}

void JournalWindow::configureTabWidget()
{
    m_tabWidget = new JournalTabWidget(this);
    connect(m_tabWidget, SIGNAL(tabCloseRequested(int)), m_control, SLOT(closeDocument(int)));
    setCentralWidget(m_tabWidget);
}

void JournalWindow::setupActions()
{
    KStandardAction::quit(kapp, SLOT(quit()), actionCollection());
    KStandardAction::open(m_control, SLOT(openFile()), actionCollection());
    KStandardAction::save(m_control, SLOT(saveFile()), actionCollection());
    KStandardAction::saveAs(m_control, SLOT(saveFileAs()), actionCollection());
    KStandardAction::openNew(m_control, SLOT(newDocument()), actionCollection());
    KStandardAction::undo(m_control, SLOT(undo()), actionCollection());
    KStandardAction::redo(m_control, SLOT(redo()), actionCollection());

    m_toolActionGroup = new QActionGroup(this);
    connect(m_toolActionGroup, SIGNAL(triggered(QAction*)), m_control, SLOT(toolChanged(QAction*)));

    // TODO: Add more actions or move them somewhere else.
    KAction *action = actionCollection()->addAction("set-tool-pen");
    action->setText("Pen");
    action->setIcon(KIcon("draw-freehand"));
    action->setActionGroup(m_toolActionGroup);
    action->setCheckable(true);
    action->setChecked(true);

    action = actionCollection()->addAction("set-tool-eraser");
    action->setText("Eraser");
    action->setIcon(KIcon("draw-eraser"));
    action->setActionGroup(m_toolActionGroup);
    action->setCheckable(true);

    // Zoom-actions
    action = actionCollection()->addAction("zoom-width", m_control, SLOT(zoomWidth()));
    action->setText("Zoom to fit width");
    action->setIcon(KIcon("zoom-fit-width"));

    action = actionCollection()->addAction("zoom-in", m_control, SLOT(zoomIn()));
    action->setText("Zoom in a bit");
    action->setIcon(KIcon("zoom-in"));

    action = actionCollection()->addAction("zoom-orig", m_control, SLOT(zoomOrig()));
    action->setText("Zoom to original size");
    action->setIcon(KIcon("zoom-original"));

    action = actionCollection()->addAction("zoom-out", m_control, SLOT(zoomOut()));
    action->setText("Zoom out a bit");
    action->setIcon(KIcon("zoom-out"));

    action = actionCollection()->addAction("zoom-fit", m_control, SLOT(zoomFit()));
    action->setText("Zoom to fit page");
    action->setIcon(KIcon("zoom-fit-best"));

    // Pen types
    m_penSizeGroup = new QActionGroup(this);
    connect(m_penSizeGroup, SIGNAL(triggered(QAction*)), m_control, SLOT(penSizeChanged(QAction*)));

    action = actionCollection()->addAction("set-size-small");
    action->setText("Small");
    action->setIcon(KIcon("battery-040"));
    action->setActionGroup(m_penSizeGroup);
    action->setCheckable(true);

    action = actionCollection()->addAction("set-size-medium");
    action->setText("Medium");
    action->setIcon(KIcon("battery-060"));
    action->setActionGroup(m_penSizeGroup);
    action->setCheckable(true);

    action = actionCollection()->addAction("set-size-big");
    action->setText("Big");
    action->setIcon(KIcon("battery-100"));
    action->setActionGroup(m_penSizeGroup);
    action->setCheckable(true);

}

JournalTabWidget* JournalWindow::getTabWidget()
{
    return m_tabWidget;
}
